package com.warehouseprovider.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.warehouseprovider.dao.StorehouseMapper;
import entity.ResponseMessage;
import entity.StatusCode;
import entity.Storehouse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import service.WarehouseService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class StorehouseServiceImpl implements WarehouseService {
    private Logger logger=Logger.getLogger(this.getClass());

    @Resource
    public StorehouseMapper storehouseMapper;

    @Override
    /**
     * 查询所有的仓库
     * 在有条件的情况下 type 根据仓库类型进行查询
     * pageNo:当前页码
     * pageSize:页容量
     */
    public ResponseMessage findAllStorehouse(Storehouse storehouse,int pageNo,int pageSize) {
        logger.info("StorehouseServiceImpl findAllStorehouse is start....   param:storehouse==>"+storehouse+",pageNo,"+pageNo+",pageSize:"+pageSize);
        ResponseMessage msg=new ResponseMessage();
        List<Storehouse> list=new ArrayList<>();
        try {
            PageHelper.startPage(pageNo,pageSize);
            list=storehouseMapper.findAllStorehouse(storehouse);
            if(list==null){
                logger.error("查询仓库信息失败！param：storehouse:"+storehouse+",pageNO:"+pageNo+",pagesize:"+pageSize);
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("查询仓库信息失败");
                return msg;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("StorehouseServiceImpl findAllStorehouse is error...",e);
            msg.setFlag(false);
            msg.setCode(StatusCode.ERROR);
            return msg;
        }
        for(int j=0;j<list.size();j++){
            if(list.get(j).getType()==1){
                list.get(j).setTypeName("总库");
            }else if(list.get(j).getType()==2){
                list.get(j).setTypeName("分库");
            }
        }
        logger.debug("查询仓库信息成功！params:"+list);
        msg.setCode(StatusCode.OK);
        msg.setFlag(true);
        msg.setMessage("查询仓库信息成功！");
        msg.setData(new PageInfo<>(list));
        return msg;
    }

    /**
     * 根据id 查询仓库的信息
     * @param id
     * @return
     */
    @Override
    public ResponseMessage getidStorehouse(String id) {
        logger.info("StorehouseServiceImpl getidStorehouse id:"+id);
        ResponseMessage msg=new ResponseMessage();
        Storehouse storehouse=new Storehouse();
        try {
            storehouse=storehouseMapper.getidStorehouse(id);
            if(storehouse==null){
                logger.error("根据id查询仓库失败！param:"+id);
                msg.setFlag(false);
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("根据id查询仓库失败！");
                return msg;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("StorehouseServiceImpl getidStorehouse is error",e);
            msg.setFlag(false);
            msg.setCode(StatusCode.ERROR);
            return msg;
        }
        logger.debug("根据id查询仓库成功！result:"+storehouse);
        msg.setCode(StatusCode.OK);
        msg.setFlag(true);
        msg.setMessage("根据id查询仓库信息成功！");
        msg.setData(storehouse);
        return msg;
    }

    /**
     * 根据仓库的id修改仓库信息
     * @param storehouse
     * @return
     */
    @Override
    public ResponseMessage xgStorehouse(Storehouse storehouse) {
        logger.info("StorehouseServiceImpl storehouse:"+storehouse);
        ResponseMessage msg=new ResponseMessage();
        int i=0;//查询过来的结果
        try {
           i=storehouseMapper.xgStorehouse(storehouse);
           if(i==0){
               logger.error("StorehouseServiceImpl storehouse is error,param:"+storehouse);
               msg.setCode(StatusCode.ERROR);
               msg.setMessage("根据id修改仓库信息失败！");
               msg.setFlag(false);
               return msg;
           }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("StorehouseServiceImpl storehouse is error",e);
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            return msg;
        }
        logger.debug("StorehouseServiceImpl storehouse is success,result:"+i);
        msg.setFlag(true);
        msg.setMessage("根据id修改仓库信息成功！");
        msg.setCode(StatusCode.OK);
        msg.setData(i);
        return msg;
    }

    /**
     * 查询总仓库
     * @return
     */
    @Override
    public ResponseMessage getQueryStronger() {
        logger.info("StorehouseServiceImpl getQueryStronger is start.....");
        ResponseMessage msg=new ResponseMessage();
        List<Storehouse> list=new ArrayList<>();
        try {
            list=storehouseMapper.getQueryStronger();
            if(list==null){
                logger.error("StorehouseServiceImpl getQueryStronger is error,param:"+list);
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("查询总库失败！");
                msg.setFlag(false);
                return msg;
            }
        } catch (Exception e) {
            logger.error("StorehouseServiceImpl getQueryStronger is error",e);
            e.printStackTrace();
            msg.setFlag(false);
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("查询总库失败!进入catch");
            return msg;
        }
        logger.debug("StorehouseServiceImpl getQueryStronger is success,result:"+list);
        msg.setMessage("查询总库成功！");
        msg.setCode(StatusCode.OK);
        msg.setFlag(true);
        msg.setData(list);
        return msg;
    }

    /**
     * 增加仓库
     * @param storehouse
     * @return
     */
    @Override
    public ResponseMessage insertStronger(Storehouse storehouse) {
        logger.info("StorehouseServiceImpl insertStronger storehouse:"+storehouse);
        ResponseMessage msg=new ResponseMessage();
        int i=0;
        try {
          i=storehouseMapper.insertStronger(storehouse);
          if(i==0){
              logger.error("StorehouseServiceImpl insertStronger is error,param:"+storehouse);
              msg.setCode(StatusCode.ERROR);
              msg.setMessage("新增仓库信息失败！");
              msg.setFlag(false);
              return msg;
          }
        } catch (Exception e) {
            logger.error("StorehouseServiceImpl insertStronger is error",e);
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("新增仓库失败！进入catch");
            return msg;
        }
        logger.debug("StorehouseServiceImpl insertStronger is success,result:"+i);
        msg.setFlag(true);
        msg.setMessage("新增仓库成功！");
        msg.setCode(StatusCode.OK);
        msg.setData(i);
        return msg;
    }
}
